//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "EarlyExercise.h"
using namespace Cephei::QL;
#include <gen/QL/Exercise.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::CEarlyExercise::CEarlyExercise (QL::Exercise::TypeEnum type, Microsoft::FSharp::Core::FSharpOption<Boolean>^ payoffAtExpiry) : CExercise(CEarlyExercise::typeid)
{
    try
    {
#ifdef HANDLE
        _phEarlyExercise = NULL;
#endif
        QuantLib::Exercise::Type _type = (QuantLib::Exercise::Type)type ;
        bool _payoffAtExpiry = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (payoffAtExpiry) ? (bool)ValueHelper::Convert (payoffAtExpiry->Value) : false); //4
        _ppEarlyExercise = new boost::shared_ptr<QuantLib::EarlyExercise> (new QuantLib::EarlyExercise ( _type,  _payoffAtExpiry ));
        SetExercise (boost::dynamic_pointer_cast<QuantLib::Exercise> (*_ppEarlyExercise));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::CEarlyExercise::CEarlyExercise (boost::shared_ptr<QuantLib::EarlyExercise>& childNative, Object^ owner) : CExercise(CEarlyExercise::typeid)
{
#ifdef HANDLE
	_phEarlyExercise = NULL;
#endif
	_ppEarlyExercise = &childNative;
    _ppExercise = new boost::shared_ptr<QuantLib::Exercise> (boost::dynamic_pointer_cast<QuantLib::Exercise> (*_ppEarlyExercise));
}
Cephei::QL::CEarlyExercise::CEarlyExercise (QuantLib::EarlyExercise& childNative, Object^ owner) : CExercise(CEarlyExercise::typeid)
{
#ifdef HANDLE
	_phEarlyExercise = NULL;
#endif
	_ppEarlyExercise = new boost::shared_ptr<QuantLib::EarlyExercise> (&childNative);
    _ppExercise = new boost::shared_ptr<QuantLib::Exercise> (boost::dynamic_pointer_cast<QuantLib::Exercise> (*_ppEarlyExercise));
    _EarlyExerciseOwner = owner;
    _ExerciseOwner = owner;
}

Cephei::QL::CEarlyExercise::CEarlyExercise (CEarlyExercise^ copy) : CExercise(CEarlyExercise::typeid)
{
#ifdef HANDLE
	_phEarlyExercise = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppEarlyExercise = new boost::shared_ptr<QuantLib::EarlyExercise> (copy->GetShared());
        _ppExercise = new boost::shared_ptr<QuantLib::Exercise> (boost::dynamic_pointer_cast<QuantLib::Exercise> (*_ppEarlyExercise));
    }
}
Cephei::QL::CEarlyExercise::CEarlyExercise (PLATFORM::Type^ t) : CExercise(CEarlyExercise::typeid)
{
#ifdef HANDLE
	_phEarlyExercise = NULL;
#endif
	if (!t->IsSubclassOf(CEarlyExercise::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CEarlyExercise::CEarlyExercise (QuantLib::Handle<QuantLib::EarlyExercise>& childNative, Object^ owner)  : CExercise(CEarlyExercise::typeid)
{
	_phEarlyExercise = &childNative;
	_ppEarlyExercise = &static_cast<boost::shared_ptr<QuantLib::EarlyExercise>>(childNative.currentLink());
    _ppExercise = new boost::shared_ptr<QuantLib::Exercise> (boost::dynamic_pointer_cast<QuantLib::Exercise> (*_ppEarlyExercise));
    _EarlyExerciseOwner = owner;
}
Cephei::QL::CEarlyExercise::CEarlyExercise (QuantLib::Handle<QuantLib::EarlyExercise> childNative)  : CExercise(CEarlyExercise::typeid)
{
	_phEarlyExercise = &childNative;
	_ppEarlyExercise = &static_cast<boost::shared_ptr<QuantLib::EarlyExercise>>(childNative.currentLink());
    _ppExercise = new boost::shared_ptr<QuantLib::Exercise> (boost::dynamic_pointer_cast<QuantLib::Exercise> (*_ppEarlyExercise));
}
#endif
#ifdef STRUCT
Cephei::QL::CEarlyExercise::CEarlyExercise (QuantLib::EarlyExercise childNative)  : CExercise(CEarlyExercise::typeid)
{
#ifdef HANDLE
	_phEarlyExercise = NULL;
#endif
	_ppEarlyExercise = new boost::shared_ptr<QuantLib::EarlyExercise> (new QuantLib::EarlyExercise (childNative));
    _ppExercise = new boost::shared_ptr<QuantLib::Exercise> (boost::dynamic_pointer_cast<QuantLib::Exercise> (*_ppEarlyExercise));
}
#endif

Cephei::QL::CEarlyExercise::~CEarlyExercise ()
{
    if (_ppEarlyExercise != NULL)
    {
	    delete _ppEarlyExercise;
        _ppEarlyExercise = NULL;
    }
}
Cephei::QL::CEarlyExercise::!CEarlyExercise ()
{
    if (_ppEarlyExercise != NULL)
    {
	    delete _ppEarlyExercise;
    }
}
QuantLib::EarlyExercise& Cephei::QL::CEarlyExercise::GetReference ()
{
    if (_ppEarlyExercise == NULL) throw REFNEW NativeNullException ();
	return **_ppEarlyExercise;
}
boost::shared_ptr<QuantLib::EarlyExercise>& Cephei::QL::CEarlyExercise::GetShared ()
{
    if (_ppEarlyExercise == NULL) throw REFNEW NativeNullException ();
	return *_ppEarlyExercise;
}
QuantLib::EarlyExercise* Cephei::QL::CEarlyExercise::GetPointer ()
{
    if (_ppEarlyExercise == NULL) throw REFNEW NativeNullException ();
	return &**_ppEarlyExercise;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::EarlyExercise>& Cephei::QL::CEarlyExercise::GetHandle ()
{
	if (_phEarlyExercise == NULL)
	{
		_phEarlyExercise = new Handle<QuantLib::EarlyExercise> (*_ppEarlyExercise);
	}
	return *_phEarlyExercise;
}
#endif
bool Cephei::QL::CEarlyExercise::HasNative () 
{
	return (_ppEarlyExercise != NULL);
}

Boolean Cephei::QL::CEarlyExercise::PayoffAtExpiry::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppEarlyExercise)->payoffAtExpiry ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::IEarlyExercise^ Cephei::QL::CEarlyExercise_Factory::Create (QL::Exercise::TypeEnum type, Microsoft::FSharp::Core::FSharpOption<Boolean>^ payoffAtExpiry)
{
    return REFNEW CEarlyExercise ( type,  payoffAtExpiry);
}
